#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<stopwidget.h>

#include<QPainter>
#include<QPixmap>
#include<QDialog>
#include<QPushButton>
#include<QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(680,920);


    dlg=new StopWidget(this);


    connect(ui->btn_start,&QToolButton::clicked,this,&MainWindow::on_pushButton_clicked);
    //connect(ui->btn_restart,&QToolButton::clicked,this,&MainWindow::on_pushButton_2_clicked);

    connect(ui->widget,&GameWidget::value,this,[=](int value){
            ui->label->setText(QString::number(value));
        });

    connect(ui->btn_stop,&QToolButton::clicked,ui->widget,[=](){
        ui->widget->timer.stop();
        //ui->btn_start->setText("返回游戏");
        dlg->show();
    });

    connect(dlg,&StopWidget::on_btn_continue_clicked,[=](){
        dlg->close();
        ui->widget->timer.start();
    });

    connect(dlg,&StopWidget::on_btn_back_clicked,[=](){
        dlg->close();
        on_btn_home_clicked();
    });

    connect(dlg,&StopWidget::on_btn_restart_clicked,this,[=](){
        dlg->close();
        MainWindow::on_pushButton_2_clicked();
    });

    connect(ui->btn_home,&QToolButton::clicked,[=](){
        //this->close();
        on_btn_home_clicked();
    });

    connect(ui->actionstart,&QAction::triggered,ui->btn_start,&QToolButton::click);
    connect(ui->actionrestart,&QAction::triggered,this,&MainWindow::on_pushButton_2_clicked);
    connect(ui->actionstop,&QAction::triggered,ui->btn_stop,&QToolButton::click);
    connect(ui->actionback,&QAction::triggered,ui->btn_home,&QToolButton::click);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/Img/bg2.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    painter.fillRect(pix.rect(),QColor(0,0,0,150));


}



void MainWindow::on_pushButton_clicked()
{
    ui->widget->start();
    ui->widget->setFocus();
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->widget->restart();
    ui->widget->setFocus();
}
